package cs.xwi7xwa.activities;

import java.io.IOException;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.bumptech.bumpapi.BumpAPI;
import com.bumptech.bumpapi.BumpConnection;

import cs.xwi7xwa.R;
import cs.xwi7xwa.data.BumpResources;
import cs.xwi7xwa.data.ExhibitDbAdapter;
import cs.xwi7xwa.data.FactDbAdapter;
import cs.xwi7xwa.templates.BumpListTemplate;

public class MyFactsView extends BumpListTemplate {
    private FactDbAdapter dbAdapter;
    private Cursor cursor;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_facts);

        // Load the database
        dbAdapter = new FactDbAdapter(this);
        try {
            dbAdapter.open();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Populate the page with the exhibits from the database
        refreshList();

        // This list should be filterable
        getListView().setTextFilterEnabled(true);
        
        // Log this method's execution
        log(null);
    }

    /**
     * Fetch exhibits from the database and list them out in this Activity
     */
    private void refreshList() {
        // Fetch all facts from the database
        cursor = dbAdapter.fetchVisibleFactExhibit();
        startManagingCursor(cursor);

        String[] displayCol = new String[] { ExhibitDbAdapter.KEY_NAME,
                FactDbAdapter.KEY_DESC, FactDbAdapter.KEY_NUMBER };

        int[] toView = new int[] { R.id.tradeItemFirstLine,
                R.id.tradeItemSecondLine, R.id.tradeItemNumber };
        
        // Wrap items from cursor in layout two_line_list_item, display only the
        // data from displayCol columns, and send it over to the toView view in
        // this Activity
        setListAdapter(new SimpleCursorAdapter(this,
                R.layout.trade_list_item, cursor, displayCol, toView));
        
        // Log this method's execution
        log(null);
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position,
            long id) {
        super.onListItemClick(listView, view, position, id);

        // Load the clicked item from the database
        Cursor c = dbAdapter.fetch((int) id);
        Cursor cList = cursor;
        cList.moveToPosition(position);

        // Pass id, description, exhibit name and fact number of this item to
        // FactView
        Intent intent = new Intent(this, FactView.class);
        intent.putExtra(FactDbAdapter.KEY_ID, (int) id);
        intent.putExtra(FactDbAdapter.KEY_DESC, c.getString(c
                .getColumnIndexOrThrow(FactDbAdapter.KEY_DESC)));
        String exhibitName = cList.getString(cList.getColumnIndexOrThrow(ExhibitDbAdapter.KEY_NAME));
        intent.putExtra(ExhibitView.EXHIBIT_NAME, exhibitName);
        intent.putExtra(FactView.FACT_NUMBER,
            dbAdapter.getFactNumber((int) id,
                c.getInt(c.getColumnIndexOrThrow(FactDbAdapter.KEY_EXHIBITID))));

        startActivityForResult(intent, 0);
        
        // Log this method's execution
        log(new String[]{"Fact ID: " + id, "Exhibit Name: " + exhibitName});
    }

    public void goToTradeView(View view) {
        if (BumpResources.BUMP_INTENT == null) {
            BumpResources.BUMP_INTENT = new Intent(this, BumpAPI.class);
        }
        
        BumpResources.BUMP_INTENT.putExtra(BumpAPI.EXTRA_USER_NAME, "Xwi7xwa App User");
        BumpResources.BUMP_INTENT.putExtra(BumpAPI.EXTRA_API_KEY, BumpResources.API_KEY);
        startActivityForResult(BumpResources.BUMP_INTENT, 0);
        
        // Log this method's execution
        log(null);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK)
        {
            BumpResources.BUMP_CONNECTION = (BumpConnection) data
                    .getParcelableExtra(BumpAPI.EXTRA_CONNECTION);

            startActivityForResult(new Intent(this, BumpTradeView.class), 0);
            
            // Log this method's execution
            log(new String[]{"Success"});
        }
        else
        {
            // Log this method's execution
            log(new String[]{"Error: " + resultCode});
        }
    }
}
